package com.xyq.fs.index;

import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.poi.extractor.POITextExtractor;

import org.apache.poi.ooxml.extractor.ExtractorFactory;

import org.apache.poi.openxml4j.util.ZipSecureFile;

import org.apache.poi.xslf.usermodel.XMLSlideShow;

import org.apache.poi.xslf.usermodel.XSLFSlide;

import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;

import com.xyq.fs.constants.FieldConstant;
import com.xyq.fs.contants.filesuffixes2.PptEnums;
import com.xyq.fs.views.WindwosA;

public interface IndexFileInf {

	static long maxSize = 1024 * 1024 * 10;

	public default Document getSimDoc(Path path, String suffix, String bz) {

		Document doc = new Document();
		String fileName = path.getFileName().toString();
		// 文件路径
		StringField sf = new StringField(FieldConstant.FILE_PATH, path.toString(), Store.YES);
		doc.add(sf);
		// 盘符
		sf = new StringField(FieldConstant.FILE_PANFU, (path.getRoot().toString().charAt(0) + "").toUpperCase(),
				Store.NO);
		doc.add(sf);

		// 文件后缀
		sf = new StringField(FieldConstant.FILE_SUFFIX, suffix, Store.NO);
		doc.add(sf);
		// 文件名
		TextField tf = new TextField(FieldConstant.FILE_NAME, fileName, Store.YES);
		doc.add(tf);

		StoredField stf = new StoredField(FieldConstant.FILE_UPDATE_TIME, path.toFile().lastModified());
		// 按照最后修改时间排序
		doc.add(stf);
		// 备注排序
		if (bz != null) {
			doc.add(new StringField(FieldConstant.FILE_BZ_SORT, bz, Store.YES));
		}
		fileName = null;
		stf = null;
		tf = null;
		sf = null;
		path = null;
		bz = null;
		suffix = null;

		return doc;
	}

	public default String getContent(Path path) {

		String content = "抱歉,文件暂时无法预览";
		POITextExtractor extractor = null;
		try (InputStream bis = new FileInputStream(path.toFile());) {
			String ps = path.getFileName().toString().toUpperCase();
			if (ps.endsWith(PptEnums.PPTX)) {
				content = readPptx(path);
			} else {
				extractor = ExtractorFactory.createExtractor(bis);
				content = extractor.getText();
			}
			ps = null;
		} catch (Exception e) {
			System.out.println("无法解析:" + path);
		} finally {
			if (extractor != null)
				try {
					extractor.close();
				} catch (IOException e) {
					extractor = null;
				}
			path = null;
		}
		return content.trim();
	}

	default String readPptx(Path file) {

		try {
			if (WindwosA.getCb2().isSelected() && Files.size(file) > maxSize)
				return "抱歉，文件暂时无法预览";
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		StringBuilder sb = new StringBuilder();
		try (FileInputStream fis = new FileInputStream(file.toFile()); XMLSlideShow ss = new XMLSlideShow(fis);) {

			ZipSecureFile.setMinInflateRatio(-100d);
			List<XSLFSlide> list = ss.getSlides();
			if (list != null) {
				for (XSLFSlide slide : list) {
					CTSlide rawSlide = slide.getXmlObject();
					CTGroupShape gs = rawSlide.getCSld().getSpTree();
					@SuppressWarnings("deprecation")
					CTShape[] shapes = gs.getSpArray();
					for (CTShape shape : shapes) {
						CTTextBody tb = shape.getTxBody();
						if (null == tb) {
							continue;
						}
						@SuppressWarnings("deprecation")
						CTTextParagraph[] paras = tb.getPArray();
						for (CTTextParagraph textParagraph : paras) {
							@SuppressWarnings("deprecation")
							CTRegularTextRun[] textRuns = textParagraph.getRArray();
							for (CTRegularTextRun textRun : textRuns) {
								sb.append(textRun.getT());
							}
						}
					}
				}
				list.clear();
				list = null;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public void indexFile(Path path, String bz);

}
